package boot.service.impl.wxShopping;

import boot.dao.GoodsShopcartMapper;
import boot.domain.wxGoodsCart.GoodsShopcart;
import boot.domain.wxGoodsCart.GoodsShopcartExample;
import boot.service.wxShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
    public class wxShoppingServiceImpl implements wxShoppingService {
    @Autowired
    private GoodsShopcartMapper goodsShopcartMapper;

    @Override
    public int add(int goodId, int num, int userId) {
        GoodsShopcartExample goodsShopcartExample = new GoodsShopcartExample();
        GoodsShopcartExample.Criteria criteria = goodsShopcartExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodId);
        criteria.andUserIdEqualTo(userId);
        criteria.andStatusNotEqualTo((byte) 2);
        if (goodsShopcartMapper.selectByExample(goodsShopcartExample).size() == 0) {
            int t = 1;
            byte status = (byte) t; // Ok
            GoodsShopcart goodsShopcart = new GoodsShopcart(null, userId, goodId, num, status);
            goodsShopcartMapper.insert(goodsShopcart);
            return goodsShopcart.getId();
        } else {
            GoodsShopcart goodsShopcart = goodsShopcartMapper.selectByExample(goodsShopcartExample).get(0);
            GoodsShopcart goodsShopcart1 = new GoodsShopcart();
            goodsShopcart1.setNum(goodsShopcart.getNum() + 1);
            goodsShopcartMapper.updateByExampleSelective(goodsShopcart1, goodsShopcartExample);
            return goodsShopcart.getId();
        }
    }

    @Override
    public List<GoodsShopcart> getCarts(int userId) {
        GoodsShopcartExample goodsShopcartExample = new GoodsShopcartExample();
        GoodsShopcartExample.Criteria criteria = goodsShopcartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andStatusNotEqualTo((byte) 2);
        return goodsShopcartMapper.selectByExample(goodsShopcartExample);
    }

    @Override
    public GoodsShopcart getCartById(int Id) {
        return goodsShopcartMapper.selectByPrimaryKey(Id);
    }

    @Override
    public List<GoodsShopcart> CommitOrder(int userId) {
        GoodsShopcartExample goodsShopcartExample = new GoodsShopcartExample();
        GoodsShopcartExample.Criteria criteria = goodsShopcartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andStatusEqualTo((byte) 1);
        List<GoodsShopcart> list = goodsShopcartMapper.selectByExample(goodsShopcartExample);
        GoodsShopcart goodsShopcart = new GoodsShopcart(null, null, null, null, (byte) 2);
        try {
            goodsShopcartMapper.updateByExampleSelective(goodsShopcart, goodsShopcartExample);

            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }


    }

    @Override
    public List<GoodsShopcart> getOrder(int userId) {
        GoodsShopcartExample goodsShopcartExample = new GoodsShopcartExample();
        GoodsShopcartExample.Criteria criteria = goodsShopcartExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andStatusEqualTo((byte) 2);
        return goodsShopcartMapper.selectByExample(goodsShopcartExample);
    }

    @Override
    public int changeGoodNum(int userId, int goodId, int num) {
        GoodsShopcartExample goodsShopcartExample = new GoodsShopcartExample();
        GoodsShopcartExample.Criteria criteria = goodsShopcartExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodId);
        criteria.andUserIdEqualTo(userId);
        GoodsShopcart goodsShopcart1 = new GoodsShopcart();
        goodsShopcart1.setNum(num);
        goodsShopcartMapper.updateByExampleSelective(goodsShopcart1, goodsShopcartExample);
        return 1;
    }

    @Override
    public int deleteGood(int userId, int goodId) {
        GoodsShopcartExample goodsShopcartExample = new GoodsShopcartExample();
        GoodsShopcartExample.Criteria criteria = goodsShopcartExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodId);
        criteria.andUserIdEqualTo(userId);
        try {
            goodsShopcartMapper.deleteByExample(goodsShopcartExample);
            return 1;
        } catch (Exception e) {
            return 0;
        }


    }

    @Override
    public int goodChecked(int userId, int goodId, Byte status) {
        GoodsShopcartExample goodsShopcartExample = new GoodsShopcartExample();
        GoodsShopcartExample.Criteria criteria = goodsShopcartExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodId);
        criteria.andUserIdEqualTo(userId);
        GoodsShopcart goodsShopcart1 = new GoodsShopcart();
        goodsShopcart1.setStatus(status);
        try {
            goodsShopcartMapper.updateByExampleSelective(goodsShopcart1, goodsShopcartExample);
            return 1;
        } catch (Exception e) {
            return 0;
        }


    }

    @Override
    public int setCartStatus(int cartId) {
        GoodsShopcartExample goodsShopcartExample = new GoodsShopcartExample();
        GoodsShopcartExample.Criteria criteria = goodsShopcartExample.createCriteria();
        criteria.andIdEqualTo(cartId);
        GoodsShopcart goodsShopcart = new GoodsShopcart(null,null,null,null,(byte) 2);
        goodsShopcartMapper.updateByExampleSelective(goodsShopcart,goodsShopcartExample);
        return 1;
    }

}
